package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.pojo.DTO.BalanceDTO;
import com.example.demo.pojo.DTO.GoodsDocDTO;
import com.example.demo.pojo.param.CallbackParam;
import com.example.demo.pojo.param.PullStockParam;
import com.example.demo.service.IBalanceService;
import com.example.demo.service.IProductService;
import com.example.demo.web.Token;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("post")
@Api(tags = "POST管理模块")
public class PostController {

    private static final String JZTORGID = "O3CUM7GN6JI";
    private static final String JZTPTCODE = "W010";

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Token token;
    @Autowired
    private IProductService productService;
    @Autowired
    private IBalanceService balanceService;

    @PostMapping("pushGoods")
    @ApiOperation("商品资料上传")
    @ApiOperationSupport(order = 100)
    public JSON pushGoods(){
        String url = "https://wdlm.test.jk.com/erp-api/wdlm/wholesale/pushGoodsDoc";
        GoodsDocDTO goodsDocDTO = new GoodsDocDTO();
        goodsDocDTO.setProductList(productService.list());
        String s = JSON.toJSONString(goodsDocDTO);
        JSONObject jsonObject = JSONObject.parseObject(s);

        HttpHeaders headers = new HttpHeaders();
        headers.set("token", token.getToken());
        headers.set("ptCode",JZTPTCODE);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<JSONObject> entity = new HttpEntity<>(jsonObject, headers);
        String json = restTemplate.exchange(url, HttpMethod.POST,entity,String.class).getBody();
        return JSON.parseObject(json);
    }

    @PostMapping("pushBalance")
    @ApiOperation("库存上传")
    @ApiOperationSupport(order = 200)
    public JSON pushBalance(){
        String url = "https://wdlm.test.jk.com/erp-api/wdlm/wholesale/pushBalance";
        BalanceDTO balanceDTO = new BalanceDTO();
        balanceDTO.setBalanceList(balanceService.list());
        String s = JSON.toJSONString(balanceDTO);
        JSONObject jsonObject = JSONObject.parseObject(s);

        HttpHeaders headers = new HttpHeaders();
        headers.set("token", token.getToken());
        headers.set("ptCode",JZTPTCODE);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<JSONObject> entity = new HttpEntity<>(jsonObject, headers);
        String json = restTemplate.exchange(url, HttpMethod.POST,entity,String.class).getBody();
        return JSON.parseObject(json);
    }

    @PostMapping("getPullStock")
    @ApiOperation("拉取请退货数据")
    @ApiOperationSupport(order = 300)
    public JSON getPullStock(@Valid PullStockParam param){
        log.debug("开始处理《拉取请退货数据》参数：{}",param);
        String url = "https://wdlm.test.jk.com/erp-api/wdlm/wholesale/pullStockRequest";
        /**
         * restTemplate 请求其他服务（带token认证，参数格式json）
         */
//        //请求参数
//        Map<String, String> map = new HashMap<>();
//        map.put("startTime", "2023-01-01 00:00:00");
//        map.put("endTime", "2023-01-07 00:00:00");
//        map.put("yewType", "1");
//        //...组装参数
        String s = JSON.toJSONString(param);
        //转成json格式
        JSONObject jsonObject = JSONObject.parseObject(s);
        HttpHeaders headers = new HttpHeaders();
        //Header 参数
        headers.set("token", token.getToken());
        headers.set("orgid",JZTORGID);
        headers.set("ptCode",JZTPTCODE);
        headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<JSONObject> entity = new HttpEntity<>(jsonObject, headers);
        String json = restTemplate.exchange(url, HttpMethod.POST,entity,String.class).getBody();
        log.debug("处理《拉取请退货数据》完成，返回参数：{}",json);
        return JSON.parseObject(json);
    }

    //要求参数为Array
    @PostMapping("getCallback")
    @ApiOperation("请退货回调")
    @ApiOperationSupport(order = 400)
    public JSON getCallback(@Valid CallbackParam callbackParam){
        log.debug("开始处理《请退货回调》");
        String url = "https://wdlm.test.jk.com/erp-api/wdlm/wholesale/stockRequestCallback";
        /**
         * restTemplate 请求其他服务（带token认证，参数格式json）
         */
        //请求参数
//        Map<String, String> map = new HashMap<>();
//        map.put("billNo", "77686664");
//        map.put("yewType", "002");
//        map.put("status", "1");
        //...组装参数
        List<CallbackParam> list = new ArrayList();
        list.add(callbackParam);
        String s = JSON.toJSONString(list);
        //转成json格式
        JSONArray jsonArray = JSONArray.parseArray(s);
        HttpHeaders headers = new HttpHeaders();
        //Header 参数
        headers.set("token", token.getToken());
        headers.set("orgid",JZTORGID);
        headers.set("ptCode",JZTPTCODE);
        headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<JSONArray> entity = new HttpEntity<>(jsonArray, headers);
        String json = restTemplate.exchange(url, HttpMethod.POST,entity,String.class).getBody();
        log.debug("处理《请退货回调》完成，返回参数：{}",json);
        return JSON.parseObject(json);
    }

}
